package com.ease.common.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 
 * @author nathanleewei
 * 
 */
public abstract class StreamUtil extends Detect {
	public static InputStream String2InputStream(String string) {
		ByteArrayInputStream stream = new ByteArrayInputStream(string.getBytes());
		return stream;
	}

	public static String inputStream2String(InputStream inputStream) {
		return inputStream2String(inputStream, (String) null);
	}

	public static String inputStream2String(InputStream inputStream, String encoding) {
		try {
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, encoding);

			BufferedReader in = new BufferedReader(inputStreamReader);
			StringBuffer buffer = new StringBuffer();
			String line = "";
			while ((line = in.readLine()) != null) {
				buffer.append(line);
			}
			return buffer.toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
